package com.dinim.test.objectlibrary;

import java.awt.Image;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.dinim.common.StopWatch;
import com.dinim.graphics.GraphicsContextSwingImpl;
import com.dinim.graphics.IDrawable;
import com.dinim.graphics.IGraphicsContext;
import com.dinim.math.GeometryUtil;
import com.dinim.matrix.twodimensional.IPoint2D;
import com.dinim.object.IObject2D;
import com.dinim.object.Object2D;
import com.dinim.object.Rectangle2D;
import com.dinim.object.Star2D;
import com.dinim.physics2D.Aabb;
import com.dinim.physics2D.AabbSingleBoundingBox;
import com.dinim.test.TestApplication;

public class MovingObjectsWithIntersectionsTester extends TestApplication{

    private Rectangle2D rect1 = new Rectangle2D(20, 20, 55, 55);
    private Rectangle2D rect2 = new Rectangle2D(52, 20, 100, 50);
    private Rectangle2D rect3 = new Rectangle2D(160, 40, 70, 55);
    private Aabb aabb = null;
    
    private List objects = new ArrayList();
    
    public static void main(String[] args) {
        MovingObjectsWithIntersectionsTester owit = new MovingObjectsWithIntersectionsTester();
        owit.showForm();
    }

    public MovingObjectsWithIntersectionsTester(){

        for(int i = 0; i < 100; i++){
            //objects.add(new Rectangle2D(randomizer.nextInt(300), randomizer.nextInt(300), randomizer.nextInt(60), randomizer.nextInt(60)));
            int bla = randomizer.nextInt(160);
            IObject2D o = new Rectangle2D(randomizer.nextInt(width), randomizer.nextInt(height), bla, bla);
            objects.add(o);
            System.out.println(o + " ; " + o.hashCode());
        }

        Timer t = new Timer();
        t.schedule(new TimerTask(){
            StopWatch sw = new StopWatch();
            public void run() {
                sw.start();
                
                aabb = new AabbSingleBoundingBox(width, height, 15, 15);

                aabb.addObject(rect1);
                aabb.addObject(rect2);
                aabb.addObject(rect3);
                
                Iterator it = objects.iterator();
                while(it.hasNext()){
                    Object2D obj = (Object2D) it.next();
                    obj.rotate(1, obj.getCenterPoint());
                    //obj.translate(randomizer.nextInt(5)-2, randomizer.nextInt(5)-2);
                    aabb.addObject(obj);
                }

                sw.stop();
                System.out.println(sw.getElapsedTime() + "  ;  " + objects.size() + "  ;  ");
                //objects.add(new Rectangle2D(randomizer.nextInt(300), randomizer.nextInt(300), randomizer.nextInt(60), randomizer.nextInt(60)));
                //System.out.println(objects.size());
                
                //aabb.getObjectGroups();
                
                repaint();
            }
        }, 1000, 20);
    }

    public void repaint(){
        
        Image imagebuffer = this.getC().createImage(width, height);
        imagebuffer.getGraphics();
        
        IGraphicsContext gc = new GraphicsContextSwingImpl(imagebuffer.getGraphics());
        //gc.setAntialiasing(true);

        if(gc != null){

            //gc.drawFilledRectangle(0, 0, width, height);
            aabb.draw(gc);
            //System.out.println(aabb);
            Iterator it = objects.iterator();
            while(it.hasNext()){
                Object2D obj = (Object2D)it.next();
                obj.draw(gc);

                //((IDrawable)((Object2D)it.next()).getBoundingBox()).draw(gc);
                
            }
            rect1.draw(gc);
            rect2.draw(gc);
            rect3.draw(gc);
        }
        this.getC().getGraphics().drawImage(imagebuffer, 0, 0, this.getC());
    }
    
    public void paintSomething(IGraphicsContext gc) {
    }
}
